package Server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Log.LU;

public class SurveyCommit extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SurveyCommit() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String json_content=request.getParameter("json_content");
		String ip=request.getRemoteAddr();
		LU.l("SurveyCommit Servlet Recived Request From "+ip);
		LU.l("SurveyCommit Servlet Recived json_content : "+json_content);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String sqls1="INSERT INTO qanswers(userip,contents) VALUES('"+ip+"','"+json_content+"')";
		try {
			Statement sm=((Connection) getServletContext().getAttribute("con")).createStatement();
			sm.executeUpdate(sqls1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LU.e("SurveyCommit Cannot Execute Insert Into SQL Statement! The Request From Ip : "+ip+" Is DISABLE!");
			out.print("SurveyCommit Cannot Execute Insert Into SQL Statement! The Request From Ip : "+ip+" Is DISABLE!");
			out.flush();
			out.close();
			return ;
		}
		LU.l("SurveyCommit Execute Insert Into SQL Statement Succesfully! The Request From Ip : "+ip+" Handle Finish!");
		out.print("succes");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
